package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.survey.SurveyApproval;
import com.ruoyi.system.domain.survey.SurveyQuestionnaire;
import com.ruoyi.system.mapper.SurveyApprovalMapper;
import com.ruoyi.system.mapper.SurveyQuestionnaireMapper;
import com.ruoyi.system.service.ISurveyQuestionnaireService;

/**
 * 问卷调查Service实现
 * 
 */
@Service
public class SurveyQuestionnaireServiceImpl implements ISurveyQuestionnaireService 
{
    @Autowired
    private SurveyQuestionnaireMapper surveyQuestionnaireMapper;
    
    @Autowired
    private SurveyApprovalMapper surveyApprovalMapper;

    /**
     * 查询问卷
     * 
     * @param surveyId 问卷ID
     * @return 问卷
     */
    @Override
    public SurveyQuestionnaire selectSurveyQuestionnaireById(Long surveyId)
    {
        return surveyQuestionnaireMapper.selectSurveyQuestionnaireById(surveyId);
    }

    /**
     * 查询问卷列表
     * 
     * @param surveyQuestionnaire 问卷
     * @return 问卷
     */
    @Override
    public List<SurveyQuestionnaire> selectSurveyQuestionnaireList(SurveyQuestionnaire surveyQuestionnaire)
    {
        return surveyQuestionnaireMapper.selectSurveyQuestionnaireList(surveyQuestionnaire);
    }
    
    /**
     * 查询待审批问卷列表
     * 
     * @param surveyQuestionnaire 问卷
     * @return 问卷集合
     */
    @Override
    public List<SurveyQuestionnaire> selectSurveyApprovalList(SurveyQuestionnaire surveyQuestionnaire)
    {
        return surveyQuestionnaireMapper.selectSurveyApprovalList(surveyQuestionnaire);
    }

    /**
     * 新增问卷
     * 
     * @param surveyQuestionnaire 问卷
     * @return 结果
     */
    @Override
    public int insertSurveyQuestionnaire(SurveyQuestionnaire surveyQuestionnaire)
    {
        surveyQuestionnaire.setCreateTime(DateUtils.getNowDate());
        surveyQuestionnaire.setCreateBy(SecurityUtils.getUsername());
        // 默认状态为草稿
        if (surveyQuestionnaire.getStatus() == null)
        {
            surveyQuestionnaire.setStatus("0");
        }
        // 初始化访问量和答卷数量
//        if (surveyQuestionnaire.getVisitCount() == null)
//        {
//            surveyQuestionnaire.setVisitCount(0);
//        }
        if (surveyQuestionnaire.getAnswerCount() == null)
        {
            surveyQuestionnaire.setAnswerCount(0);
        }
        return surveyQuestionnaireMapper.insertSurveyQuestionnaire(surveyQuestionnaire);
    }

    /**
     * 修改问卷
     * 
     * @param surveyQuestionnaire 问卷
     * @return 结果
     */
    @Override
    public int updateSurveyQuestionnaire(SurveyQuestionnaire surveyQuestionnaire)
    {
        surveyQuestionnaire.setUpdateTime(DateUtils.getNowDate());
        surveyQuestionnaire.setUpdateBy(SecurityUtils.getUsername());
        return surveyQuestionnaireMapper.updateSurveyQuestionnaire(surveyQuestionnaire);
    }

    /**
     * 批量删除问卷
     * 
     * @param surveyIds 需要删除的问卷ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSurveyQuestionnaireByIds(Long[] surveyIds)
    {
        return surveyQuestionnaireMapper.deleteSurveyQuestionnaireByIds(surveyIds);
    }

    /**
     * 删除问卷信息
     * 
     * @param surveyId 问卷ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSurveyQuestionnaireById(Long surveyId)
    {
        return surveyQuestionnaireMapper.deleteSurveyQuestionnaireById(surveyId);
    }
    
    /**
     * 发布问卷
     * 
     * @param surveyId 问卷ID
     * @return 结果
     */
    @Override
    public int publishSurveyQuestionnaire(Long surveyId)
    {
        return surveyQuestionnaireMapper.updateSurveyStatus(surveyId, "2");
    }
    
    /**
     * 结束问卷
     * 
     * @param surveyId 问卷ID
     * @return 结果
     */
    @Override
    public int endSurveyQuestionnaire(Long surveyId)
    {
        return surveyQuestionnaireMapper.updateSurveyStatus(surveyId, "3");
    }
    
    /**
     * 提交问卷审批
     * 
     * @param surveyId 问卷ID
     * @return 结果
     */
    @Override
    public int submitSurveyApproval(Long surveyId)
    {
        // 更新问卷状态为待审核
        return surveyQuestionnaireMapper.updateSurveyStatus(surveyId, "1");
    }
    
    /**
     * 审批问卷
     * 
     * @param surveyApproval 问卷审批信息
     * @return 结果
     */
    @Override
    @Transactional
    public int approveSurveyQuestionnaire(SurveyApproval surveyApproval)
    {
        surveyApproval.setCreateTime(DateUtils.getNowDate());
        surveyApproval.setApprovalUserName(SecurityUtils.getUsername());
        surveyApproval.setApprovalUserId(SecurityUtils.getUserId());
        
        // 插入审批记录
        surveyApprovalMapper.insertSurveyApproval(surveyApproval);
        
        // 更新问卷状态
        String status = "1".equals(surveyApproval.getStatus()) ? "2" : "0"; // 通过为已发布，拒绝为草稿
        return surveyQuestionnaireMapper.updateSurveyStatus(surveyApproval.getSurveyId(), status);
    }
    
    /**
     * 更新问卷访问量
     * 
     * @param surveyId 问卷ID
     * @return 结果
     */
    @Override
    public int updateSurveyVisitCount(Long surveyId)
    {
        return surveyQuestionnaireMapper.updateSurveyVisitCount(surveyId);
    }
} 